import { getAllDrugs , deleteADrug , changeDrugStatus } from '../../../utils/api/drugControl'
import userBaseInfoState from '../../../stores/userBaseInfo'
// pages/service/drug-control/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collapseActiveNames: [],
    checked: false,
    drugList: <EatDrugAlertEntity[]>[
      {
        pdeId: 123,
        patientUid: 123,
        drugName: '阿司匹林',
        frequency: '每天3次',
        unit: '片',
        dose: 3,
        isActive: true,
        eatTime:'08:00;12:00;18:00',
      },
      {
        pdeId: 124,
        patientUid: 123,
        drugName: '阿司匹林2',
        frequency: '每天4次',
        unit: '片',
        dose: 6,
        isActive: false,
        eatTime:'07:00;11:00;18:10',
      }
    ],
    showStatus: true,
  },

  getAllDrugsList(patientUid: any) {
    console.log('用药管理页面加载，获取到的patientUid是：', patientUid);
    getAllDrugs(patientUid).then((res: any) => {
      console.log('请求成功',res);
      if (res.code === 0 && res.data.length !== 0) {
         // 转换数据格式
        const newDrugList: EatDrugAlertEntity[] = res.data.map((drug: any) => ({
          pdeId: drug.pdeId,
          patientUid: drug.patientUid,
          drugName: drug.drugName,
          frequency: drug.frequency,
          unit: drug.unit,
          dose: drug.dose,
          isActive: drug.isActive === 1,
          eatTime: drug.eatTime.join(';')
        }));
        this.setData({
          drugList: newDrugList,
          showStatus: true,
        })
      }
      else if (res.code === 0 && res.data.length === 0) {
        console.log('请求成功，但该用户没有数据');
        this.setData({
          showStatus: false,
        })
      }
    })
  },

  changeCollapseActiveNames(event: any) {
    this.setData({
      collapseActiveNames: event.detail,
    });
  },
  
  checkChange(event:any) {
    const drugListIndex = event.currentTarget.dataset.listIndex;
    // 处理传入数据
    const dataToChange = {
      pdeId: this.data.drugList[drugListIndex].pdeId,
      patientUid: this.data.drugList[drugListIndex].patientUid,
      drugName: this.data.drugList[drugListIndex].drugName,
      frequency: this.data.drugList[drugListIndex].frequency,
      unit: this.data.drugList[drugListIndex].unit,
      dose: this.data.drugList[drugListIndex].dose,
      isActive: this.data.drugList[drugListIndex].isActive,
      eatTime: this.data.drugList[drugListIndex].eatTime,
    };
    const patientUid = userBaseInfoState.data.patientUid as string;
    if (dataToChange.isActive === true) {
      dataToChange.isActive = 0;
      console.log('现在isActive是', dataToChange.isActive);
    }
    else {
      dataToChange.isActive = 1;
      console.log('现在isActive是', dataToChange.isActive);
    }
    console.log(dataToChange);
    // 发起网络请求
    changeDrugStatus(dataToChange).then((res: any) => {
      console.log('上传修改成功',res);
      this.getAllDrugsList(patientUid);
      console.log('页面更新成功');
    }).catch((err: any) => {
      console.log('修改失败', err);
    })
  },

  deleteDrug(event:any) { 
    const drugListIndex = event.currentTarget.dataset.listIndex;
    const patientUid = userBaseInfoState.data.patientUid as string;
    const pdeId = this.data.drugList[drugListIndex].pdeId as number;
    // 发送网络请求删除特定药品
    deleteADrug(pdeId).then((res: any) => {
      console.log('删除成功', res);
      //删除成功后更新页面
      this.getAllDrugsList(patientUid);
      console.log('更新页面成功');
    }).catch((err: any) => {
      console.log('删除失败', err);
    })
  },

  navigateToAddDrug() { 
    wx.navigateTo({
      url:'./add-drug/index'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const patientUid = userBaseInfoState.data.patientUid as string;
    this.getAllDrugsList(patientUid);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const patientUid = userBaseInfoState.data.patientUid as string;
    this.getAllDrugsList(patientUid);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const patientUid = userBaseInfoState.data.patientUid as string;
    this.getAllDrugsList(patientUid);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})